import { PackageUtil } from "./package-util";
import axios from "axios";

export function checkUpdate(){
    // 从远程仓库获取最新的 package.json 文件
    const packageJsonUrl = `${PackageUtil.repository}/raw/master/package.json`;
    axios.get(packageJsonUrl).then((response: { data: any; }) => {
        const latestPackageJson = response.data;
        const latestVersion = latestPackageJson.version;
        const currentVersion = PackageUtil.version;
        // 比较版本号
        if (latestVersion !== currentVersion) {
            // 弹出对话框，提示用户更新
            console.log("【HiYOOO】发现新版本：");
            console.log("【HiYOOO】当前版本：" + currentVersion);
            console.log("【HiYOOO】最新版本：" + latestVersion);
            console.log("【HiYOOO】请关闭Cocos Creator,运行 update-hiyooo-plugin进行更新");
        }else{
            console.log("【HiYOOO】当前版本为最新版本");
            console.log("【HiYOOO】当前版本：" + currentVersion);
            console.log("【HiYOOO】最新版本：" + latestVersion);
        }
    });
}

/**
 * 统计
 */
export async  function statistics(){
    // 从远程仓库获取最新的 package.json 文件
    const packageJsonUrl = `${PackageUtil.repository}/raw/master/package.json`;

    // 统计系统信息
    const os = require("os")
   
    // 统计本地ip
    const localIp = ()=>{
        const its  = os.networkInterfaces();
        for (const itKey in its) {
            for (const item of its[itKey]) {
                if (item.family === 'IPv4' && !item.internal) {
                    return item.address;
                }
            }
        }
        return "";
    }

    // 统计cpu信息
    const sys  = require("systeminformation");
    const system = await sys.system();  // 系统信息

    let param = {
        username:system.uuid,
        info:sys.info,
        ip:localIp(),
    }
    const apiPath = "http://localhost:5000"

    axios.post(apiPath,param).then((response: { data: any; }) => {})
}